// miniprogram/pages/broadcast/publish/publish.js
import {addRecord} from "../../../service/Record";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: "lost"  //lost found
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({type: options.type});
        wx.setNavigationBarTitle({
            title: options.type === "lost" ? "发布寻物启事信息" : "发布拾物认领信息"
        })
    },

    /*
    * 监听丢失发布数据
    * */
    onAddRecord: async function (e) {
        try {
            let res = await addRecord(e.detail);
            this.setData({loading: false});
            Toast.success({
                message: "发布成功",
                mask: true,
                onClose: () => {
                    wx.redirectTo({url: "/pages/user/record-detail/record-detail?id=" + res._id})
                }
            });
        } catch (e) {
            this.setData({loading: false})
            Toast.fail("发布失败");
            console.error(e);
        }
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {}
})